package com.apes.enquiry.product.repository;

import com.alibaba.fastjson.JSONObject;
import com.apes.enquiry.product.model.BrandUploading;
import com.apes.enquiry.product.model.ReplacementProduct;
import com.apes.framework.jpa.Connector;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.query.ApesDao;
import com.apes.framework.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @className: ProductDao
 * @description: 描述 替换件关系品牌关系数据批量插入
 * @author: lilonh
 * @create: 2022-11-16 16:08
 */

@Component("spProductDao")
public class ProductDao extends ApesDao {

    private String unique;

    @Autowired
    private Connector connector;

    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private ReplacementProductRepository replacementProductRepository;

    /**
     * 批量插入替换件关系
     * @param replacementProducts
     */
    public void replacementInsert(List<ReplacementProduct> replacementProducts){
        Date date = new Date();
        String sql = "insert into sp_mas_replacement_product(id, replacement, pn, brand,supplier_id,create_date) values (?,?,?,?,?,?)";
        Connection conn = connector.getConnection();
        PreparedStatement pst = null;
        Set<String> set = replacementProductRepository.findAllId();
        try {
            String id = sequenceService.getId(ReplacementProduct.CONST_TABLE_NAME);
            pst = conn.prepareStatement(sql);
            for (int i = 0; i < replacementProducts.size(); i++) {
                ReplacementProduct replacementProduct = replacementProducts.get(i);
                do {
                    id = MD5Util.encrypt(  replacementProduct.getPn() + Math.random() +replacementProduct.getBrand()+ id+new Date().getTime()).substring(0,8);
                }while (!set.add(id));
                replacementProduct.setId(id);
                pst.setString(1,id);
                pst.setString(2,replacementProduct.getReplacement());
                pst.setString(3,replacementProduct.getPn());
                pst.setString(4,replacementProduct.getBrand());
                pst.setString(5,replacementProduct.getSupplier().getId());
                pst.setDate(6,new java.sql.Date(date.getTime()));
                //添加到批处理对象
                pst.addBatch();
                //每添加500条执行一次
                if ((i+1) % 500 == 0) pst.executeBatch();
            }
            pst.executeBatch();
        } catch (SQLException throwables) {
            if (throwables.toString().contains("ORA-00001"))
                throw new RuntimeException("导入失败，原厂零件号加车品牌不能重复！");
            if (throwables.toString().contains("ORA-01400"))
                throw new RuntimeException("导入失败，必填字段不能为空！");
            throw new RuntimeException(throwables);
        } finally {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 批量插入品牌件关系
     * @param brandUploadings
     */
    public void brandUploadingInsert(List<BrandUploading> brandUploadings){
        Date date = new Date();
        String sql = "insert into sp_mas_brand_uploading(id, brand_id, brand,automobile,pn,supplier_id,create_date) values (?,?,?,?,?,?,?)";
        Connection conn = connector.getConnection();
        PreparedStatement pst = null;
        try {
            String id = sequenceService.getId(BrandUploading.CONST_TABLE_NAME);
            pst = conn.prepareStatement(sql);
            for (int i = 0; i < brandUploadings.size(); i++) {
                BrandUploading brandUploading = brandUploadings.get(i);
                id = MD5Util.encrypt(  brandUploading.getPn() + Math.random() +brandUploading.getBrand()+ id).substring(0,8);
                brandUploading.setId(id);
                pst.setString(1,id);
                pst.setString(2,brandUploading.getBrandId());
                pst.setString(3,brandUploading.getBrand());
                pst.setString(4,brandUploading.getAutomobile());
                pst.setString(5,brandUploading.getPn());
                pst.setString(6,brandUploading.getSupplier().getId());
                pst.setDate(7,new java.sql.Date(date.getTime()));
                //添加到批处理对象
                pst.addBatch();
                //每添加500条执行一次
                if ((i+1) % 500 == 0) pst.executeBatch();
            }
            pst.executeBatch();
        } catch (SQLException throwables) {
            if (throwables.toString().contains("ORA-00001"))
                throw new RuntimeException("导入失败，数据不能重复！");
            if (throwables.toString().contains("ORA-01400"))
                throw new RuntimeException("导入失败，必填字段不能为空！");
            throw new RuntimeException(throwables);
        } finally {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
